﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using digitwhu.Models;
using digitwhu.Models.IRepositorys;
namespace digitwhu.Models.Repositorys
{
    public class MessageRepository : Repository<User>, IMessageRepository
    {
        public MessageRepository(DataContext dc)
            : base(dc)
        {
        }       
     /*  public int GetAllCommentNumber(int userId) //查看当前用户收到评论数量
        {
            int oc = (from c in context.GetTable<OriginBlogComment>()
                      from b in context.GetTable<OriginalMicroBlog>()
                      where c.originalBlogId == b.originalBlogId && b.userId == userId
                      select c).Count();
            int tc = (from c in context.GetTable<TranBlogComment>()
                      from b in context.GetTable<TranMicroBlog>()
                      where c.tranMicroBlogId == b.tranMicroBlogId && b.userId == userId
                      select c).Count();
            return oc + tc;
        }*/
         
        public List<OriginalMicroBlog> GetAllOriginBlogBeCommented(int userId)//查询某个用户的被其它用户评论过的所有原创微博
        {
            //userId:指明待查询用户的编号
            //返回值：原创微博列表，可能为空
            List<OriginalMicroBlog> list = (from b in context.GetTable<OriginalMicroBlog>()
                                           where b.userId == userId && b.OriginBlogComment.Count>0
                                           select b).ToList();
            return list;
        }
        public List<TranMicroBlog> GetAllTranBlogBeCommented(int userId)//查询某个用户的被其它用户评论过的所有转发微博
        {
            //userId:指明待查询用户的编号
            //返回值：转发微博列表，可能为空
            List<TranMicroBlog> list = (from t in context.GetTable<TranMicroBlog>()
                                        where t.userId == userId && t.TranBlogComment.Count > 0
                                        select t).ToList();
            return list;
        }
        public List<OriginalMicroBlog> GetAllAtOriginBlog(int userId)//查询在原创微博或者原创微博评论中@了用户的所有原创微博
        {
            //userId:指明待查询用户编号
            //返回值：原创微博列表，可能为空
            List<OriginalMicroBlog> list = (from ao in context.GetTable<AtOriginalBlog>()
                                            from o in context.GetTable<OriginalMicroBlog>()
                                            from aoc in context.GetTable<AtOriginBlogComment>()
                                            from oc in context.GetTable<OriginBlogComment>()
                                            where (ao.userId==userId && ao.originalBlogId == o.originalBlogId) ||
                                            (aoc.userId==userId && aoc.commentId == oc.commentId && oc.originalBlogId == o.originalBlogId)
                                            select o).Distinct().ToList();                       
            return list;
        }
        public List<TranMicroBlog> GetAllAtTranMicroBlog(int userId)//查询在转发微博或者转发微博评论中@了用户的所有转发微博
        {
            //userId:指明待查询用户编号
            //返回值：转发微博列表，可能为空
            List<TranMicroBlog> list = (from at in context.GetTable<AtTranBlog>()
                                            from t in context.GetTable<TranMicroBlog>()
                                            from atc in context.GetTable<AtTranBlogComment>()
                                            from tc in context.GetTable<TranBlogComment>()
                                            where (at.userId==userId && at.tranMicroBlogId == t.tranMicroBlogId) ||
                                            (atc.userId==userId && atc.commentId == tc.commentId && tc.tranMicroBlogId == t.tranMicroBlogId)
                                            select t).Distinct().ToList();
            return list;
        }            
    }
}